package org.aplikator.client.widgets;

import org.aplikator.client.Aplikator.AplikatorResources;
import org.aplikator.client.data.Record;
import org.aplikator.client.descriptor.PropertyDTO;
import org.aplikator.client.descriptor.ViewDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RefButtonWidget extends Composite implements DataField<Integer> {
    private Button button = new Button();
    private Integer value;// TODO change to pk
    private PropertyDTO<Integer> property;
    private ViewDTO view;
    private TableInterface table;
    private DialogBox dialogBox;
    private boolean dirty;

    public RefButtonWidget(String caption, ViewDTO view) {
        HorizontalPanel panel = new HorizontalPanel();
        this.view = view;
        panel.add(button);
        button.setText(caption);
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (dialogBox == null) {
                    dialogBox = createDialogBox();
                    // dialogBox.setAnimationEnabled(true);
                    // dialogBox.center();
                }
                dialogBox.show();
                table.reload();
            }
        });
        initWidget(panel);
    }

    public PropertyDTO<Integer> getProperty() {
        return property;
    }

    public void setProperty(PropertyDTO<Integer> property) {
        this.property = property;

    }

    public void setValue(Integer value) {
        setValue(value, false);
    }

    public void setValue(Integer value, boolean fireEvents) {
        Integer oldValue = this.value;
        this.value = value;
        if (fireEvents) {
            setDirty(true);
            ValueChangeEvent.fireIfNotEqual(this, oldValue, value);
        }
    }

    public Integer getValue() {
        return value;
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Integer> handler) {
        return this.addHandler(handler, ValueChangeEvent.getType());
    }

    private DialogBox createDialogBox() {
        // Create a dialog box and set the caption text

        // Create a table to layout the content
        VerticalPanel dialogContents = new VerticalPanel();
        final DialogBox dialogBox = new DialogBox();
        dialogBox.setText("Select reference - " + view.getLocalizedName());
        dialogBox.setWidget(dialogContents);
        dialogContents.setSpacing(4);
        dialogBox.setWidget(dialogContents);
        table = TableFactory.create(view, null, null);
        table.asWidget().setSize("100%", "450px");
        dialogContents.add(table);
        // Add a close button at the bottom of the dialog
        Button OKButton = new Button("OK", new ClickHandler() {
            public void onClick(ClickEvent event) {
                dialogBox.hide();
                setValue(table.getSelectedPrimaryKey().getId(), true);
            }
        });
        dialogContents.add(OKButton);

        // dialogContents.setCellHorizontalAlignment(closeButton,
        // HasHorizontalAlignment.ALIGN_RIGHT);

        Button closeButton = new Button("Cancel", new ClickHandler() {
            public void onClick(ClickEvent event) {
                dialogBox.hide();

            }
        });
        dialogContents.add(closeButton);

        // Return the dialog box
        return dialogBox;
    }

    public Record getSelectedRecord() {
        if (table != null) {
            return table.getSelectedRecord();
        }
        return null;
    }

    public void setDirty(boolean dirty) {
        this.dirty = dirty;
        if (dirty) {
            button.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            button.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

    public boolean isDirty(){
        return dirty;
    }

}
